package com.emapp.lccode.utils

import android.app.Application
import com.emapp.lccode.bean.ProblemTypeKt
import com.tencent.mmkv.MMKV

/**
 * Author:YanshanZhang
 * E-mail:690810892@qq.com
 * Date: 2022/4/20 14:15
 * Desc:
 */
object MmkvUtil {
    private const val ID_DEFAULT = "id_default"
    private const val ID_ACCOUNT = "id_account"
    fun init(application: Application?) {
        MMKV.initialize(application)
    }

    fun from(key: String): MMKV {
        return if (key.startsWith("account_")) {
            accountMMKV
        } else {
            defaultMMKV
        }
    }

    private val defaultMMKV: MMKV
        private get() = MMKV.mmkvWithID(ID_DEFAULT, MMKV.SINGLE_PROCESS_MODE)
    private val accountMMKV: MMKV
        private get() = MMKV.mmkvWithID(ID_ACCOUNT, MMKV.SINGLE_PROCESS_MODE)

    fun clearAccountMMKV() {
        accountMMKV.clearAll()
    }

    @JvmStatic
    fun putBoolean(key: String, value: Boolean) {
        from(key).encode(key, value)
    }

    fun getBoolean(key: String, defaultValue: Boolean): Boolean {
        return from(key).decodeBool(key, defaultValue)
    }

    fun putString(key: String, value: String) {
        from(key).encode(key, value)
    }

    fun getString(key: String, defaultValue: String?): String {
        return from(key).decodeString(key, defaultValue).toString()
    }

    fun putInt(key: String, value: Int) {
        from(key).encode(key, value)
    }

    fun getInt(key: String, defaultValue: Int): Int {
        return from(key).decodeInt(key, defaultValue)
    }

    fun putFloat(key: String, value: Float) {
        from(key).encode(key, value)
    }

    fun getFloat(key: String, defaultValue: Float): Float {
        return from(key).decodeFloat(key, defaultValue)
    }

    fun putLong(key: String, value: Long) {
        from(key).encode(key, value)
    }

    fun getLong(key: String, defaultValue: Long): Long {
        return from(key).decodeLong(key, defaultValue)
    }

    fun putDouble(key: String, value: Double) {
        from(key).encode(key, value)
    }

    fun getLong(key: String, defaultValue: Double): Double {
        return from(key).decodeDouble(key, defaultValue)
    }

    fun putByteArray(key: String, value: ByteArray?) {
        from(key).encode(key, value)
    }

    fun getByteArray(key: String): ByteArray? {
        return from(key).decodeBytes(key)
    }

    fun getStringSet(key: String, value: Set<String?>?) {
        from(key).encode(key, value)
    }

    fun getStringSet(key: String): Set<String>? {
        return from(key).decodeStringSet(key)
    }

    fun setType(type: String) {
        putString("device_type", type)
    }

    fun getTypes(): ArrayList<ProblemTypeKt>? {
        val type = getString("device_type", "")
        return type.toArrayEntity<ProblemTypeKt>()
    }
//    fun putParcelable(key: String, value: Parcelable?) {
//        from(key).encode(key, value)
//    }
//
//    fun getParcelable(key: String, T: Class<*>?): Parcelable? {
//        return from(key).decodeParcelable<Parcelable>(key, T)
//    }
}